<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>_发票打印</title>
<script type="text/javascript">
var filterParam = {};
var datatable_content;
  $(function() {
		$("#queryForm").validate(); 
		$("#_fromDate").datepicker({ format:'yyyy-mm-dd'});
		$("#_toDate").datepicker({ format:'yyyy-mm-dd'});
		var now = new Date();
		var tomorrow = new Date();
		tomorrow.setDate(now.getDate()+1);
		if(!$("#_fromDate").val()){
			$("#_fromDate").val(now.Format("yyyy-MM-dd"));
		}
		if($("#_toDate").val() == ''){
			$("#_toDate").val(tomorrow.Format("yyyy-MM-dd"));
		}
  });
		function initTable(){
			$("#orderTable").hide();
			$("#contentTable").hide();
			if($("#_orderno").val()||$("#_taskCode").val()){
				$("#orderTable").show();
				datatable_content = $("#orderTable").dataTable({
				   "bServerSide":true,
				   "bAutoWidth":false,
		           "sAjaxSource":'${ctx}/v2/invoicePrint/page',
		           "fnServerData": callback,
		           "fnRowCallback":rowCallBack,
		           "bFilter": false,
		           "bSort": false,
		           "sZeroRecords": "查询无记录！",
		           "bProcessing": true,
		           "oLanguage": {
		               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
		            },
		             "aoColumns":[
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": "batchCode"},
		             	 {"mData": "taskCode"},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": "order.ordeno"},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             ],
		             "aoColumnDefs":[
		             	{"aTargets": [1],
			              "mRender": function (data, display, row) {
			              		<c:forEach items="${customerList}" var="customer">
									if("${customer.customerCode}" == row.customerCode){
										return "${customer.customerName}";
									}
								</c:forEach>
								return "";
				           }
			            },
			            {"aTargets": [2],
			              "mRender": function (data, display, row) {
			              		<c:forEach items="${warehouseList}" var="warehouse">
									if("${warehouse.groupCode}" == row.warehouseCode){
										return "${warehouse.groupName}";
									}
								</c:forEach>
								return "";
				           }
			            },
			            {"aTargets": [5],
			              "mRender": function (data, display, row) {
			              		return row.goodsNum == 1 ? "单件" : row.goodsNum > 1 ? "多件" :"";
				           }
			            },
			            {"aTargets": [6],
			              "mRender": function (data, display, row) {
			              		return row.billType == "cbill" ? "普通运单" : row.billType == "ebill" ? "电子面单" :"";
				           }
			            },
			            {"aTargets": [7],
			              "mRender": function (data, display, row) {
			              		return row.flowType == "prePrint" ? "预先打单" : row.flowType == "intimePrint" ? "及时打单" :"";
				           }
			            },
			            {"aTargets": [9],
			              "mRender": function (data, display, row) {
								return row.createTime ? new Date(row.createTime.time).Format("yyyy-MM-dd") : "";
				           }
			            },
			            {"aTargets": [10],
			              "mRender": function (data, display, row) {
			              	if(row.needInvoice==0){
			              			return "不打印发票";
			              		}else{
			              			return row.invoiceNo  ? "已打印发票" : "未打印发票" ;
			              		}
				          	}
			            },
			            {"aTargets": [11],
			              "mRender": function (data, display, row) {
								var s ="";
								if(row.needInvoice != 0){
									s = row.invoiceNo ? "<button  class=\"btn btn-primary\" id=\"btn_invoice_"+row.id+"\" onclick=\"rePrintInvoiceByOrderNo('"+row.order.ordeno+"');\"  >重打发票</button>" 
										: "<button  class=\"btn btn-primary\" id=\"btn_invoice_"+row.id+"\"  onclick=\"printInvoiceOneNormal('"+row.order.ordeno+"',this);\" >打印发票</button>" ;
									s = s + "<a href=\"#invoiceUpdate\" data-toggle=\"modal\" role=\"button\" onclick=\"initOrderNo('"+row.order.ordeno+"')\" class=\"btn btn-primary\" >修正发票</a>";	
								}
								return s;
				           }
			            },
		             ]
				});
			}else{
				$("#contentTable").show();
				datatable_content = $("#contentTable").dataTable({
				   "bServerSide":true,
				   "bAutoWidth":false,
		           "sAjaxSource":'${ctx}/v2/invoicePrint/page',
		           "fnServerData": callback,
		           "fnRowCallback":rowCallBack,
		           "bFilter": false,
		           "bSort": false,
		           "sZeroRecords": "查询无记录！",
		           "bProcessing": true,
		           "oLanguage": {
		               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
		            },
		             "aoColumns":[
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": "batchCode"},
		             	 {"mData": "taskCode"},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             	 {"mData": null},
		             ],
		             "aoColumnDefs":[
		             	{"aTargets": [1],
			              "mRender": function (data, display, row) {
			              		<c:forEach items="${customerList}" var="customer">
									if("${customer.customerCode}" == row.customerCode){
										return "${customer.customerName}";
									}
								</c:forEach>
								return "";
				           }
			            },
			            {"aTargets": [2],
			              "mRender": function (data, display, row) {
			              		<c:forEach items="${warehouseList}" var="warehouse">
									if("${warehouse.groupCode}" == row.warehouseCode){
										return "${warehouse.groupName}";
									}
								</c:forEach>
								return "";
				           }
			            },
			            {"aTargets": [5],
			              "mRender": function (data, display, row) {
			              		return row.pickType == "single" ? "单件" : row.pickType == "multi" ? "多件" :"";
				           }
			            },
			            {"aTargets": [6],
			              "mRender": function (data, display, row) {
			              		return row.billType == "cbill" ? "普通运单" : row.billType == "ebill" ? "电子面单" :"";
				           }
			            },
			            {"aTargets": [7],
			              "mRender": function (data, display, row) {
			              		return row.flowType == "prePrint" ? "预先打单" : row.flowType == "intimePrint" ? "及时打单" :"";
				           }
			            },
			            {"aTargets": [8],
			              "mRender": function (data, display, row) {
								return row.createTime ? new Date(row.createTime.time).Format("yyyy-MM-dd") : "";
				           }
			            },
			            {"aTargets": [9],
			              "mRender": function (data, display, row) {
								return row.invoiceStatus == "1" ? "已打印发票" : row.invoiceStatus == "0" ? "未打印发票" :"";
				           }
			            },
			            {"aTargets": [10],
			              "mRender": function (data, display, row) {
								return row.invoiceStatus == "1" ? "<button  class=\"btn btn-primary\" id=\"btn_invoice_"+row.id+"\" onclick=\"rePrintInvoiceByTaskCode('"+row.taskCode+"');\"  >重打发票</button>" 
								: row.invoiceStatus == "0" ? "<button  class=\"btn btn-primary\" id=\"btn_invoice_"+row.id+"\"  onclick=\"ajaxInvoiceCount(this,'"+row.taskCode+"');\" >打印发票</button>" :"";
				           }
			            },
		             ]
				});
			}
		}
		function filter(item){
			filterParam[item.name] = item;
		}
		function callback(sSource, aoData, fnCallback ) {
		   	var formParam = $("#queryForm").serializeArray();
			for(var n in filterParam){
				formParam.push(filterParam[n]);
			}
		   $.ajax({ 
		       "type": "post",
		       "contentType": "application/json", 
		       "url": sSource,    
		       "dataType": "json",   
		       "data": JSON.stringify(aoData.concat(formParam)),
		       "success": fnCallback  
		   });
		}
		
		/**
		*创建了行的回调
		*nRow为当前的tr
		*/
		function rowCallBack(nRow, aData, iDisplayIndex, iDisplayIndexFull){
			var iDisplayStart = datatable_content.fnSettings()._iDisplayStart;
			$("td:eq(0)", nRow).html(iDisplayStart+iDisplayIndex+1);
		}
		
		function load(){
			if($("#queryForm").valid()){
				if (datatable_content) {
					datatable_content.fnDestroy();
				}
				initTable();
			}
		}
		
		function rePrintInvoiceByTaskCode(taskCode){
			location.href="${ctx}/v2/invoicePrint/rePrintInvoiceByTaskCode/"+taskCode;
		}
		function rePrintInvoiceByOrderNo(orderNo){
			location.href="${ctx}/v2/invoicePrint/rePrintInvoiceByOrderNo/"+orderNo;
		}
		function initOrderNo(orderNo){
			$("#currOrderno").val(orderNo);
			$("#invoiceNoUpdate").val("");
		}
		//更新发票号
  		function updateInvoiceNo(){
  			if($("#inputForm").valid()){
	  			$.ajax({ 
			       "type": "post",
			       "url": "${ctx}/v2/invoicePrint/changePrintInvoiceStatus",    
			       "dataType": "json",   
			       "data": {
			       		orderNO:$("#currOrderno").val(),
			       		invoiceNo:$("#invoiceNoUpdate").val(),
			       		invoiceOperate:$("#invoiceNoOperate").val(),
			       		deleteFlg:0,
			       		returnCode:0
			       },
			       "success": function(data){
			       		jAlert("单据["+$("#currOrderno").val()+"]:"+data.content,"温馨提示");
			       }  
			   });
		   }
  		}
</script>
</head>

<body>
<div id="fullbg"></div>
<div id="dialog">
	<div>
		<img src="${ctx }/static/022.gif"></img>
	</div>
</div>
<section class="content">
<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">条件查询</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
    <form class="form-search" id="queryForm" method="post" action="${ctx}/print/queryPrintList" >
    
    <div class="form-group row">
			<div class="col-sm-3">
				<div class="input-group col-sm-12">
					<span class="input-group-text"> 商家名称： </span> 
					<select class="form-control" id="_customerCode" name="search_EQ_customerCode" onchange="ajaxAreaGroup(this,'_warehouseCode')">
						<option value=""></option>
						<c:forEach items="${customerList}" var="customer">
							<option value="${customer.customerCode}"
								<c:if test="${param.search_EQ_customerCode eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
						</c:forEach>
					</select>
				</div>
			</div>
			
			
			<div class="col-sm-3">
				<div class="input-group col-sm-12">
					<span class="input-group-text"> 作业仓库： </span> 
					<select class="form-control required" id="_warehouseCode" name="search_EQ_warehouseCode">
						<option value=""></option>
						<c:forEach items="${warehouseList}" var="wm">
							<option value="${wm.groupCode}"
							<c:if test="${param._warehouseCode eq wm.groupCode}">selected=selected</c:if>>${wm.groupName}</option>
						</c:forEach>
					</select>
				</div>
			</div>
			
			<div class="col-sm-3">
				<div class="input-group col-sm-12">
					<span class="input-group-text">是否打印： </span> 
					<select class="form-control" id="_printStatus" name="search_EQ_invoiceStatus">
						<option value="" >全选</option>
			        <option value="1" >已打印</option>
			        <option value="0" selected="selected">未打印</option>
					</select>
				</div>
			</div>
			
			<div class="col-sm-3">
				<div class="input-group col-sm-12">
					<span class="input-group-text">任务编号: </span>
					<input type="text" id="_taskCode" name="search_EQ_taskCode" value="${param.search_EQ_taskCode}"  class="form-control " >
				</div>
			</div>
			
		</div>
		
		
		<div class="form-group row">
			<div class="col-sm-4">
				<div class="input-group col-sm-12">
					<span class="input-group-text"> 创建日期： </span>
						<div class="input-group">
							<div class="input-group-addon">
								<i class="fa fa-calendar"></i>
							</div>
							<input type="text" class="form-control pull-right required" id="_fromDate" name="search_GTE_createTime" value="${param.search_GTE_createTime}"/>
						</div>
				</div>
			</div>
			
			<div class="col-sm-4">
				<div class="input-group col-sm-12">
					<span class="input-group-text">至： </span>
						<div class="input-group">
							<div class="input-group-addon">
								<i class="fa fa-calendar"></i>
							</div>
							<input type="text" class="form-control pull-right required" id="_toDate" name="search_LT_createTime" value="${param.search_LT_createTime}"/>
						</div>
				</div>
			</div>
			
			<div class="col-sm-3">
				<div class="input-group col-sm-12">
					<span class="input-group-text">订单编号: </span>
					<input type="text" id="_orderno" name="search_EQ_order.ordeno" value="${param.search_EQ_orderno}"  class="form-control " >
				</div>
			</div>
					<button class="btn btn-primary" type="button" onclick="load()">搜索</button>
		</div>	
		 
	</form>
	</div>
	</div>
	</div>
	</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">查询结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
	<table id="contentTable" class="table table-striped table-bordered table-condensed" style="display: none;">
		<thead><tr>
		        <th>序号</th>
		        <th>商家</th>
			    <th>仓库</th>
			    <th>批次编号</th>
			    <th>任务编号</th>
			    <th>订单类型</th>
			    <th>运单类型</th>
			    <th>作业类型</th>
			    <th>创建时间</th>
			    <th>状态</th>
			    <th>操作</th>
			</tr></thead>
		<tbody>
		</tbody>
	</table>
	<table id="orderTable" class="table table-striped table-bordered table-condensed" style="display: none;">
		<thead><tr>
		        <th>序号</th>
		        <th>商家</th>
			    <th>仓库</th>
			    <th>批次编号</th>
			    <th>任务编号</th>
			    <th>订单类型</th>
			    <th>运单类型</th>
			    <th>作业类型</th>
			    <th>订单号</th>
			    <th>创建时间</th>
			    <th>状态</th>
			    <th>操作</th>
			</tr></thead>
		<tbody>
		</tbody>
	</table>
	</div>
	</div>
	</div>
	</div>
	<!-- 更新窗口 -->
	<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" id="invoiceUpdate" >
		<div class="modal-dialog" style="width:600px; height:500px;">
		<div class="modal-content">
		<div class="modal-header">
			<h4>更新发票号</h4>
		</div>
		
		<div class="modal-body">
		<form id="inputForm"  method="post" class="form-horizontal">
			<fieldset>
			<input type="hidden" id="currOrderno" name="currOrderno">
				<!-- 行1 -->
				<div class="form-group row">
					<div class="col-sm-6">
						<div class="input-group col-sm-12">
							<label class="input-group-text">发票号:</label> <input
								type="text" id="invoiceNoUpdate" name="invoiceNoUpdate"
								class="form-control required">
						</div>
					</div>
					<div class="col-sm-6">
						<div class="input-group col-sm-12">
							<label class="input-group-text">开票人:</label> <input
								type="text" id="invoiceNoOperate" name="invoiceNoOperate"
								class="form-control required">
						</div>
					</div>
				</div>
			</fieldset>
		</form>
		</div>
		<div class="modal-footer bg-gray">
			<button id="btn_update" class="btn btn-primary" data-dismiss="modal" aria-hidden="true" onclick="updateInvoiceNo()">更新</button>
		</div>
	</div>
	</div>
	</div>
	
	</section>
</body>
</html>
